#!/usr/bin/python
# -*- coding: utf-8 -*-

#$ -l h_rt=00:10:00
#$ -l virtual_free=100M
#$ -j y
#$ -o $HOME/conmebol.out
#$ -N conmebol

import sys
sys.path.append('/home/artem/pywikipedia')
#sys.path.append('/home/artem/Dropbox/wiki/bot/pywikipedia')

import wikipedia

def GetDate():
	import datetime
	
	return datetime.date.today()

def GetTable():
	import urllib2
	
	from BeautifulSoup import BeautifulSoup
	
	url = u'http://df1.conmebol.com/ranking/ranking_completo.html'
	
	print u"Retrieving " + url
	webpage = urllib2.urlopen(url)
	print u"\nDone!"
	
	soup = BeautifulSoup(webpage)
	
	tbody = soup.find(attrs={'class': 'tbody'}).findAll('tr')
	
	table = []
	for tr in tbody:
		tds = tr.findAll('td')
		table.append([tds[0].span.contents[0].replace('&ordm;',''), tds[1].span.contents[0], tds[1].span.span.contents[0], tds[2].span.contents[0]])
	return table
	
def RuWikification():
	import re
	site = wikipedia.getSite('ru', 'wikipedia')
	
	page = wikipedia.Page(site, u'Шаблон:Рейтинг клубов КОНМЕБОЛ/Замена')
	text = page.get()
	
	wikipedia.stopme()
	
	dict = {}
	for line in text.splitlines():
		try:
			en_name = re.findall('(?<=:).*(?=[=])', line)[0]
			ru_name = re.findall('(?<=[=]).*', line)[0]
			dict[en_name] = ru_name
		except:
			continue
		
	return dict

def MakeContent(table, date):
	ru_wikification = RuWikification()
	
	content = u"{{Шаблон:Рейтинг клубов КОНМЕБОЛ/Шапка}}\n"
	for item in table:
		content += u"{{Шаблон:Рейтинг клубов КОНМЕБОЛ/Строка|" + item[0] + "|"
		if item[1] in ru_wikification:
			content	+= ru_wikification[item[1]]
		else:
			content	+= item[1]
		content += "|" + item[2].replace('(','{{').replace(')','}}') + "|" + item[3].replace('.', ',') + "}}\n"
	content += u"{{Шаблон:Рейтинг клубов КОНМЕБОЛ/Подвал|" + str(date.strftime('{{date|%d|%m|%Y|2}}')).decode('utf8') + "}}"
	
	content += u"<noinclude>\n{{doc}}\n[[Категория:Википедия:Шаблоны:Футбол]]</noinclude>"
	
	return content
	
def WriteToWP(content):
	site = wikipedia.getSite('ru', 'wikipedia')
	
	page = wikipedia.Page(site, u'Шаблон:Рейтинг клубов КОНМЕБОЛ')
	page.put(content, comment=u'Обновление рейтинга клубов КОНМЕБОЛ')

	wikipedia.stopme()

date = GetDate()

table = GetTable()
content = MakeContent(table, date)

WriteToWP(content)
